<template>
  <div>
    <div style="margin-bottom: 30px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>销售广告管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <h1>广告列表</h1>
    <el-divider></el-divider>
    <el-button type="primary" @click="add" class="mb-5"> 添加广告<i class="el-icon-plus"></i></el-button>
    <el-card class="box-card" v-for="advertisement in advertisements" shadow="hover">
      <div>
        <span><h2>{{ advertisement.adTitle }}</h2></span>
        <div style="float: right">
        <el-button type="success" style="margin-left: 100px" @click="edit(advertisement)" class="mr-5">编辑
          <i class="el-icon-edit"></i></el-button>
        <el-popconfirm
            confirm-button-text='确定'
            cancel-button-text='再想想'
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @confirm="del(advertisement.id)">
          <el-button type="danger" slot="reference">删除<i class="el-icon-delete"></i></el-button>
        </el-popconfirm>
        </div>
        <br>
        <span style="font-size: 12px">发布时间：{{ advertisement.adTime }}</span>
      </div>
    </el-card>
    <el-dialog
        title="广告信息"
        :visible.sync="dialogVisible"
        width="30%">
      <el-form label-width="80px" :model="advertisement" :rules="rules" ref="advertisement">
        <el-form-item label="广告标题">
          <el-input v-model="advertisement.adTitle"></el-input>
        </el-form-item>
        <el-form-item label="广告内容">
          <el-input v-model="advertisement.adText" type="textarea" :rows="10"></el-input>
        </el-form-item>
        <el-form-item label="广告图片" label-width="80px" prop="">
          <el-upload
              class="avatar-uploader"
              action="http://localhost/file/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess1"
              :before-upload="beforeAvatarUpload">
            <img v-if="advertisement.adPicture" :src="advertisement.adPicture" class="avatar" alt="">
            <img v-else-if="avatarUrl" :src="avatarUrl" class="avatar" alt="">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer" >
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save('advertisement')">{{ btnText }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Advertisement",
  data() {
    return {
      advertisements: [],
      advertisement: {},
      dialogVisible:false,
      btnText:'',
      avatarUrl: '',
      rules:{}
    }
  },
  created() {
    this.load()
  },
  methods:{
    load() {
      request.get("/ad/getAll").then(res => {
        this.advertisements = res.data.records
      })
    },
    add(){
      this.advertisement={}
      this.dialogVisible=true
      this.btnText='添加'
    },
    edit(advertisement){
      this.advertisement=Object.assign({},advertisement)
      this.dialogVisible=true
      this.btnText='修改'
    },
    del(id){
      request.delete("ad/delmore/" + id).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    save(advertisement) {
      this.$refs[advertisement].validate((valid) => {
        if (valid) {
          this.dialogVisible = false
          if (this.btnText==='添加'){
            request.post("ad/add", this.advertisement).then(res => {
              if (res) {
                this.avatarUrl = ""
                this.$message.success(this.btnText + "成功")
                this.load()
              } else {
                this.$message.error(this.btnText + "失败")
              }
            })
          }else{
            request.put("ad/update", this.advertisement).then(res => {
              if (res) {
                this.avatarUrl = ""
                this.$message.success(this.btnText + "成功")
                this.load()
              } else {
                this.$message.error(this.btnText + "失败")
              }
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleAvatarSuccess1(res, file) {//处理头像
      this.avatarUrl = URL.createObjectURL(file.raw);
      this.advertisement.adPicture = res
    },
    beforeAvatarUpload(file) {
      const isJPG_PNG = file.type === 'image/jpeg' || file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG_PNG) {
        this.$message.error('上传广告图片只能是 JPG/PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传广告图片大小不能超过 2MB!');
      }
      return isJPG_PNG && isLt2M;
    }
  }
}
</script>

<style scoped>
.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px;
}

.avatar-uploader:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>